#include "Splitter.h"
#include "Logger/tri_logger.hpp"
#include "MainWindow.h"
#include "TextEditor.h"
#include <QAction>

Splitter::Splitter(MainWindow *parent)
    : QSplitter(parent){
	TRI_LOG_STR("Constructor ");
	main_window = parent;
	work_editor = new TextEditor(this);
	
	observe_editor = new TextEditor(this);

	addWidget (work_editor);
	QWidget* dummy = new QWidget(this);
	dummy->setMinimumWidth(10);
	dummy->setMaximumWidth(10);
	addWidget(dummy);
	addWidget (observe_editor);
}

Splitter::~Splitter(){
}

void Splitter::init(){
	work_editor->setFocus();
	currentCharFormatChanged(work_editor->currentCharFormat ());
	work_editor->setDocument(main_window->document_manager->work_dokument);
	observe_editor->setDocument(main_window->document_manager->observe_document);
}


void Splitter::currentCharFormatChanged(const QTextCharFormat & f){
	TRI_LOG_STR("formatChanged");
	main_window->fontChanged(f.font());
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		main_window->alignmentChanged(t->alignment());
	}
	
	
}
void Splitter::setBold(){
	TRI_LOG_STR("setBold()");	
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		//TRI_LOG_STR("focus worked");	
		t->setBold(main_window->action_text_bold->isChecked());
	}
		
}
void Splitter::setItalic(){
	TRI_LOG_STR("setItalic()");	
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		t->setItalic(main_window->action_text_italic->isChecked());
	}
}
void Splitter::setUnderline(){
	TRI_LOG_STR("setUnderline()");
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		t->setUnderline(main_window->action_text_underline->isChecked());
	}
}
void Splitter::setTextFamily(const QString& familyIdentifier){
	TRI_LOG_STR("setTextFamily");	
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		t->setTextFamily(familyIdentifier);
	}
}
void Splitter::setTextSize(const QString& sizeIdentifier){
	TRI_LOG_STR("setTextSize");	
	TextEditor* t = (TextEditor*)focusWidget ();
	if (t){
		t->setTextSize(sizeIdentifier);
	}
}
void Splitter::setAlign(QAction* action){
	TextEditor* t = (TextEditor*)focusWidget ();
	if (!t){
		return;
	}
	if (action == main_window->action_align_left){
		t->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
	}	
	else if (action == main_window->action_align_center){	
		t->setAlignment(Qt::AlignHCenter);
	}	
	else{
		t->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
	}
}

